package com.lccviolet;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author LiChaochao
 * @Date 2021/9/7 18:37
 * @Description AtomicReferenceDemo ABA问题
 */
public class AtomicReferenceDemo {
    public static void main(String[] args) {
        User hua = new User("hua", 28);
        User niao = new User("niao", 18);

        AtomicReference<User> atomicReference = new AtomicReference<>();
        atomicReference.set(hua);

        System.out.println(atomicReference.compareAndSet(hua, niao) + "\t" + atomicReference.get().toString());
        System.out.println(atomicReference.compareAndSet(hua, niao) + "\t" + atomicReference.get().toString());
    }
}

@Getter
@ToString
@AllArgsConstructor
class User {
    String username;
    int age;
}
